
import os

def get_filtered_files_with_paths(directory, file_extensions=None, filename_contains=None, include_folders=False):
    """
    获取指定目录的文件列表和绝对路径列表，并支持文件名后缀过滤、文件名关键词过滤和是否包含文件夹的功能。
    
    :param directory: str - 要扫描的目录路径
    :param file_extensions: list[str] - 要过滤的文件后缀列表，例如 ['.txt', '.log'] (默认为 None，不过滤后缀)
    :param filename_contains: list[str] - 要包含在文件名中的关键词列表，例如 ['error', 'log'] (默认为 None，不过滤关键词)
    :param include_folders: bool - 是否包含目录 (默认 False，仅返回文件)
    :return: tuple[list[str], list[str]] - (文件名列表, 文件绝对路径列表)
    """
    try:
        # 保存结果的列表
        file_names = []
        absolute_paths = []

        # 遍历目录内容
        for entry in os.scandir(directory):
            # 包含目录选项
            if include_folders and entry.is_dir():
                file_names.append(entry.name)
                absolute_paths.append(os.path.abspath(entry.path))
                continue
            
            # 跳过非文件
            if not entry.is_file():
                continue

            # 文件后缀过滤
            if file_extensions:
                if not any(entry.name.endswith(ext) for ext in file_extensions):
                    continue
            
            # 文件名关键词过滤
            if filename_contains:
                if not any(keyword in entry.name for keyword in filename_contains):
                    continue

            # 添加符合条件的文件
            file_names.append(entry.name)
            absolute_paths.append(os.path.abspath(entry.path))

        return file_names, absolute_paths

    except FileNotFoundError:
        print(f"Error: The directory '{directory}' does not exist.")
        return [], []
    except PermissionError:
        print(f"Error: Permission denied for directory '{directory}'.")
        return [], []



if __name__ == "__main__":
    # 示例目录路径
    directory_path = "/path/to/directory"

    # 获取所有文件，不过滤
    print("All files:")
    file_names, absolute_paths = get_filtered_files_with_paths(directory_path)
    print("File Names:", file_names)
    print("Absolute Paths:", absolute_paths)

    # 获取指定后缀的文件
    print("\nFiles with .txt or .log extensions:")
    file_names, absolute_paths = get_filtered_files_with_paths(directory_path, file_extensions=['.txt', '.log'])
    print("File Names:", file_names)
    print("Absolute Paths:", absolute_paths)

    # 获取包含关键词的文件
    print("\nFiles containing 'error' or 'log':")
    file_names, absolute_paths = get_filtered_files_with_paths(directory_path, filename_contains=['error', 'log'])
    print("File Names:", file_names)
    print("Absolute Paths:", absolute_paths)

    # 获取所有文件和文件夹
    print("\nAll files and folders:")
    file_names, absolute_paths = get_filtered_files_with_paths(directory_path, include_folders=True)
    print("File Names:", file_names)
    print("Absolute Paths:", absolute_paths)

    # 综合过滤：后缀 + 关键词
    print("\nFiles with .txt extension and containing 'log':")
    file_names, absolute_paths = get_filtered_files_with_paths(directory_path, file_extensions=['.txt'], filename_contains=['log'])
    print("File Names:", file_names)
    print("Absolute Paths:", absolute_paths)
